package com.server.server_a10.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.server.server_a10.entity.Inf.LightIdInf;
import com.server.server_a10.entity.lightingSystem.Light;
import com.server.server_a10.management.entity.EquipmentInf;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectProvider;

import java.util.List;

@Mapper
public interface LightMapper extends BaseMapper<Light> {
    @SelectProvider(type = ProviderLight.class, method = "getLightByRoomId")
    List<Light> getLightByRoomId(@Param("room_id") int room_id);

    @SelectProvider(type = ProviderLight.class,method = "getAllLightIdByHomeIdOrRoomId")
    List<LightIdInf> getAllLightIdByHomeIdOrRoomId(@Param("id") int id,@Param("type") String type);

    @SelectProvider(type = ProviderLight.class, method = "getLightLimit")
    List<EquipmentInf> getLightLimit(String name, String room_id, String home_id, int index, int limit);

    @SelectProvider(type = ProviderLight.class, method = "getLightLimitCount")
    int getLightLimitCount(String name,String room_id,String home_id);

    class ProviderLight {
        public static String getLightByRoomId() {
            return "select * from light where room_id=#{room_id}";
        }
        public static String getAllLightIdByHomeIdOrRoomId(int id,String type){
            return "select h.id as homeId, r.id as roomId, light.id as lightId\n" +
                    "from light\n" +
                    "         left join room r on r.id = light.room_id\n" +
                    "         left join home h on r.home_id = h.id\n" +
                    "    where ${type}=#{id}";
        }

        public static String getLightLimit(){
            return "select l.id as id,l.name as name,l.room_id as room_id,r.home_id as home_id,u.id as user_id,u.name as user_name\n" +
                    "from light l\n" +
                    "    left join room r on r.id = l.room_id\n" +
                    "    left join home h on r.home_id = h.id\n" +
                    "    left join user u on h.master_id = u.id\n" +
                    "where l.id like concat('%',#{name},'%') and  l.room_id like concat('%',#{room_id},'%') and r.home_id like concat('%',#{home_id},'%')\n" +
                    "limit #{index},#{limit}";
        }

        public static String getLightLimitCount(){
            return "select count(*)\n" +
                    "from light l\n" +
                    "         left join room r on r.id = l.room_id\n" +
                    "         left join home h on r.home_id = h.id\n" +
                    "         left join user u on h.master_id = u.id\n" +
                    "where l.id like concat('%',#{name},'%') and  l.room_id like concat('%',#{room_id},'%') and r.home_id like concat('%',#{home_id},'%')";
        }
    }
}
